<?php

/**
 * Created by PhpStorm.
 * User: raymon
 * Date: 17-8-23
 * Time: 下午5:21
 */
class Article extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->_add_style_sheet("dataTables", base_url("assets/DataTables/css/dataTables.bootstrap4.min.css"));
        $this->_add_script_file("dataTables", base_url("assets/DataTables/js/jquery.dataTables.min.js"));
        $this->_add_script_file("dataTables Bootstrap", base_url("assets/DataTables/js/dataTables.bootstrap.min.js"));

        $this->load->model('base_model');
        $this->load->model('mod/article_model');

        $this->auth('admin');
        $this->sqlTable['main'] = 'article';
        $this->sqlTable['classify']['classify'] = 'article_classify';
        $this->data_common['myurl'] = array(
            'index' => site_url('mod/article/admin'),
            'add' => site_url('mod/article/admin/add'),
            'edit' => site_url('mod/article/admin/edit/'),
            'json' => site_url('mod/article/admin/json'),
            'classify' => site_url('mod/article/admin/classify'),
            'classifyAdd' => site_url('mod/article/admin/classify/add'),
            'classifyJSON' => site_url('mod/article/admin/classify/classifyJSON'),
            'classifyChange' => site_url('mod/article/admin/classify/classifyChange'),
            'classifyDel' => site_url('mod/article/admin/classify/classifyDel'),
            'json' => site_url('mod/article/admin/json')
        );
    }

    public function index()
    {
        $data = array();
        $this->_view('mod/article/admin/article.twig', $data, 'admin', 'mod/article/admin');
    }
    public function json(){
        $json = array();
        $data = $this->article_model->get_articleList();

        $json["data"] = &$data;
        print_r(json_encode($json));
    }
    public function add(){
        $data = array();
        if ($this->input->post()) {
            $this->load->library('form_validation');
            $return = array();
            if ($this->form_validation->run("article") == FALSE) {
                $return['state'] = false;
                $return['data'] = validation_errors();
            } else {
                $post = $this->input->post();
                $post['releaseTime'] = $post['releaseTime'].":00";
                if ($this->base_model->add($this->sqlTable['main'] , $post)) {
                    $return['state'] = true;
                    $return['data'] = $this->classesBg('success', '文章发布成功！');
                } else {
                    $return['state'] = false;
                    $return['data'] = $this->classesBg('dismissible', '数据更新失败！');
                }
            }
            print_r(json_encode($return));//输出json
        }else{
            $this->_add_style_sheet("AdminLTE Colorpicker Skin", base_url("assets/AdminLTE-2.3.11/plugins/colorpicker/bootstrap-colorpicker.min.css"));
            $this->_add_style_sheet("bootstrap Datepicker", base_url("assets/bootstrap/plugins/datetimepicker/css/bootstrap-datetimepicker.min.css"));

            $this->_add_script_file("AdminLTE Colorpicker Plugins", base_url("assets/AdminLTE-2.3.11/plugins/colorpicker/bootstrap-colorpicker.min.js"));
            $this->_add_script_file("bootstrap Datepicker", base_url("assets/bootstrap/plugins/datetimepicker/js/bootstrap-datetimepicker.min.js"));
            $this->_add_script_file("bootstrap Datepicker zh-CN", base_url("assets/bootstrap/plugins/datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js"));
            $this->_add_script_file("wangEditor", base_url("assets/wangEditor-3.0.8/release/wangEditor.js"));

            $data['article_classifys'] = $this->article_model->get_classify();
            $this->_view('mod/article/admin/add.twig', $data, 'admin', 'mod/article/admin');
        }

    }
    public function edit($id = '')
    {
        $data = array();
        $input = $this->article_model->get($this->sqlTable['main'], $id);
        if (empty($input)) {
            die('<div class="alert alert-danger alert-dismissible"><i class="icon fa fa-ban"></i>该文章不存在!</div>');
        }
        if ($this->input->post()) {
            $this->load->library('form_validation');
            $return = array();
            if ($this->form_validation->run("article") == FALSE) {
                $return['state'] = false;
                $return['data'] = validation_errors();
            } else {
                $post = $this->input->post();
                $post['releaseTime'] = $post['releaseTime'].":00";
                if ($this->base_model->update($this->sqlTable['main'] , $id , $post)) {
                    $return['state'] = true;
                    $return['data'] = $this->classesBg('success', '文章发布成功！');
                } else {
                    $return['state'] = false;
                    $return['data'] = $this->classesBg('dismissible', '数据更新失败！');
                }
            }
            print_r(json_encode($return));//输出json
        } else {
            $data['input'] = &$input;
            $this->_add_style_sheet("AdminLTE Colorpicker Skin", base_url("assets/AdminLTE-2.3.11/plugins/colorpicker/bootstrap-colorpicker.min.css"));
            $this->_add_style_sheet("bootstrap Datepicker", base_url("assets/bootstrap/plugins/datetimepicker/css/bootstrap-datetimepicker.min.css"));

            $this->_add_script_file("AdminLTE Colorpicker Plugins", base_url("assets/AdminLTE-2.3.11/plugins/colorpicker/bootstrap-colorpicker.min.js"));
            $this->_add_script_file("bootstrap Datepicker", base_url("assets/bootstrap/plugins/datetimepicker/js/bootstrap-datetimepicker.min.js"));
            $this->_add_script_file("bootstrap Datepicker zh-CN", base_url("assets/bootstrap/plugins/datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js"));
            $this->_add_script_file("wangEditor", base_url("assets/wangEditor-3.0.8/release/wangEditor.js"));
            $this->_add_script_file("htmlCode", base_url("assets/js/htmlcode.js"));

            $data['article_classifys'] = $this->article_model->get_classify();
            $this->_view('mod/article/admin/edit.twig', $data, 'admin', 'mod/article/admin');
        }
    }
    public function classify()
    {
        $data = array();
        $this->_view('mod/article/admin/classify.twig', $data, 'admin', 'mod/article/admin');
    }

    public function classifyAdd()
    {
        $data = array();
        if ($this->input->post()) {
            $this->load->library('form_validation');
            $return = array();
            $this->form_validation->set_rules('name', '分类名', 'trim|required|max_length[16]');
            if ($this->form_validation->run() == FALSE) {
                $return['state'] = false;
                $return['data'] = validation_errors();
            } else {
                $post = $this->input->post();
                if ($this->base_model->add($this->sqlTable['classify'], $post)) {
                    $return['state'] = true;
                    $return['data'] = $this->classesBg('success', '新建文章分类成功！');
                } else {
                    $return['state'] = false;
                    $return['data'] = $this->classesBg('dismissible', '数据更新失败！');
                }
            }
            print_r(json_encode($return));//输出json
        } else {
            $this->_view('mod/article/admin/classify-add.twig', $data);
        }
    }
    public function classifyJSON()
    {
        $data = $this->base_model->get_all($this->sqlTable['classify']);
        $json["data"] = &$data;
        print_r(json_encode($json));
    }
    public function classifyChange($id = '')
    {
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('', ''); //去掉错误信息的<p>定界符
        $return = array();
        $this->form_validation->set_rules('name', '分类名', 'trim|required|max_length[16]');
        if ($this->form_validation->run() == FALSE) {
            $return['state'] = false;
            $return['data'] = validation_errors();
        } else {
            $post = $this->input->post();
            if ($this->base_model->update($this->sqlTable['classify'], $id, $post)) {
                $return['state'] = true;
            } else {
                $return['state'] = false;
                $return['data'] = '数据更新失败!';
            }
        }
        print_r(json_encode($return));
    }
    public function classifyDel($id = '')
    {
        $data = array();
        $attack = $this->base_model->get($this->sqlTable['classify'], $id);
        if (empty($attack)) {
            exit('<div class="alert alert-danger alert-dismissible"><i class="icon fa fa-ban"></i>严重错误，该文章分类不存在!</div>');
        }
        if ($this->input->post()) {
            $return = array();
            if ($this->base_model->del($this->sqlTable['classify'], $id)) {
                $return['state'] = true;
                $return['data'] = '删除成功！';
            } else {
                $return['state'] = false;
                $return['data'] = $this->classesBg('dismissible', '数据更新失败！');
            }
            print_r(json_encode($return));
        } else {
            $data['input'] = &$attack;
            $this->_view('mod/article/admin/classify-del.twig', $data);
        }
    }
}